// pages/findowerns/findowerns.js
//引入请求代码
var call = require("../../utils/ajax.js")
var that;
let ol = 0;
var app = getApp();
// var ImgURL = "";
var petSelectList = require("../../utils/petSelectList.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        brands: [],
        objectArray: [{
                brand: "福州",
                id: 0,
                array: ['鼓楼区', '台江区', '仓山区', '马尾区', '晋安区', '福清市', '长乐市', '闽侯县', '连江县', '罗源县', '闽清县', '永泰县']
            },
            {
                brand: "厦门",
                id: 1,
                array: ['思明区', '海沧区', '湖里区', '集美区', '同安区', '翔安区']
            },
            {
                brand: "漳州",
                id: 2,
                array: ['芗城区', '龙文区', '龙海市', '漳浦县', '南靖县', '云霄县', '平和县', '华安县', '长泰县', '诏安县', '东山县']
            },
            {
                brand: "龙岩",
                id: 3,
                array: ['新罗区', '漳平市', '长汀县', '永定县', '上杭县', '武平县', '连城县']
            },
            {
                brand: "宁德",
                id: 4,
                array: ['蕉城区', '东侨新区', '福安市', '福鼎市', '霞浦县', '古田县', '屏南县', '寿宁县', '周宁县', '柘荣县']
            },
            {
                brand: "平潭",
                id: 5,
                array: ['潭城', '苏澳', '流水', '澳前', '北厝', '平原', '敖东']
            },
            {
                brand: "泉州",
                id: 6,
                array: ['丰泽区', '鲤城区', '洛江区', '泉港区', '石狮市', '晋江市', '南安市', '惠安县', '安溪县', '永春县', '德化县', '金门县']
            },
            {
                brand: "三明",
                id: 7,
                array: ['梅列区', '三元区', '永安市', '明溪县', '清流县', '宁化县', '大田县', '尤溪县', '沙县', '将乐县', '泰宁县', '建宁县']
            },
            {
                brand: "莆田",
                id: 8,
                array: ['城厢区', '涵江区', '荔城区', '秀屿区', '仙游县']
            },
            {
                brand: "南平",
                id: 8,
                array: ['延平区', '邵武市', '武夷山市', '建瓯市', '建阳市', '顺昌县', '浦城县', '光泽县', '松溪县', '政和县']
            }
        ],
        object: [],
        brandindex: 0,
        index1: 0,
        currentData: 0,
        dates: '2019-10-01',
        times: '12:00',
        catList: [],
        doglList: [],
        petItems: [{
                name: '喵星人',
                value: '喵星人',
            },
            {
                name: '汪星人',
                value: '汪星人',
            }
        ],
        sexItems: [{
                name: '男孩子',
                value: '男孩子',
            },
            {
                name: '女孩子',
                value: '女孩子',
            },
        ],
        images: [],
        uploadedImages: [],
        ImgURL: "",
        search_pet_name: "",
        search_pet_age: "",
        // search_pet_contact: "",
        search_pet_message: "",
        search_pet_area: "",
        publisher_phone: "",
        lose_city_area: "",
        search_pet_sex: "",
        lose_county_area: "",
        lose_detailed_area: "",
        special_advice: ""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        try {
            wx.removeStorageSync('fileName')
           } catch (e) {
                // Do something when catch error 
               }
               if(!app.globalData.userState){
                wx.showToast({
                    title: '您未登录！',
                    icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
                    duration: 2000     
                  }) 
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1, // 回退前 delta(默认为1) 页面
                    })
                }, 1000)
            }
        ol = 0;
        let catList = petSelectList.catList();
        let doglList = petSelectList.doglList();
        this.setData({
            catList: catList,
            doglList: doglList
        })
        var objectArray = this.data.objectArray
        var brands = []
        for (var i = 0; i < objectArray.length; i++) {
            brands.push(objectArray[i].brand)
        }
        this.setData({
            brands: brands,
            object: objectArray[this.data.brandindex].array,
            lose_city_area: objectArray[this.data.brandindex].brand,
            lose_county_area: this.data.objectArray[this.data.brandindex].array[this.data.index1]
        })
    },
    bindPickerChangecity: function(e) {
        this.setData({
            brandindex: e.detail.value,
            index1: 0
        })
        var objectArray = this.data.objectArray
        this.setData({
            object: objectArray[this.data.brandindex].array
        })

        // console.log(this.data.brands[this.data.brandindex])
        this.setData({
            lose_city_area: this.data.brands[this.data.brandindex]
        })
        console.log(this.data.lose_city_area)
        this.bindPickerChangearea(0)
        // console.log(this.data.brandindex)
        //     console.log(objectArray[this.data.brandindex].array[this.data.index1])
    },
    bindPickerChangearea: function(e) {
        let that = this;
        if(e.detail.value!=undefined&&e.detail.value!=""){
            that.setData({
                index1: e.detail.value
            })
            that.setData({
                    lose_county_area: that.data.objectArray[that.data.brandindex].array[that.data.index1]
                })
                // console.log(this.data.objectArray[this.data.brandindex].array[this.data.index1])
            console.log(that.data.lose_county_area)
        }else{
            that.setData({
                index1: e
            })
            that.setData({
                    lose_county_area: that.data.objectArray[that.data.brandindex].array[that.data.index1]
                })
                // console.log(this.data.objectArray[this.data.brandindex].array[this.data.index1])
            console.log(that.data.lose_county_area)
        }

    },

    bindTimeChange: function(e) {
        console.log(e.detail.value)
        this.setData({
            times: e.detail.value
        })
    },
    //  点击日期组件确定事件  
    bindDateChange: function(e) {
        console.log(e.detail.value)
        this.setData({
            dates: e.detail.value
        })
    },

    radio: function(e) {

        var index = e.currentTarget.dataset.index; //获取当前点击的下标
        var petItems = this.data.petItems; //选项集合
        var sexItems = this.data.sexItems;
        if (petItems[index].checked) return; //如果点击的当前已选中则返回
        petItems.forEach(item => {
            item.checked = false
        })
        petItems[index].checked = true; //改变当前选中的checked值
        this.setData({
            petItems: petItems
        });


    },
    bindPickerChange: function(e) {
        var that = this;
        that.setData({
            value: that.data.catList[e.detail.value].name,
            Index: e.detail.value
        })
        console.log('picker发送选择改变，携带值为', that.data.catList[e.detail.value].name)
    },
    bindPickerChange1: function(e) {
        var that = this;
        that.setData({

            value: that.data.doglList[e.detail.value].name,
            Index: e.detail.value
        })
        console.log('picker发送选择改变，携带值为', that.data.doglList[e.detail.value].name)
    },
    radio_sex: function(e) {
        var index = e.currentTarget.dataset.index;
        var sexItems = this.data.sexItems;
        if (sexItems[index].checked) return; //如果点击的当前已选中则返回
        sexItems.forEach(item => {
            item.checked = false
        })
        sexItems[index].checked = true; //改变当前选中的checked值
        this.setData({
            sexItems: sexItems
        });
    },

    radioChange: function(e) {
        var checkValue = e.detail.value;
        this.setData({
            checkValue: checkValue
        });
        console.log(checkValue)
        var that = this
        if (checkValue == '喵星人') {
            that.setData({
                hideAdd: 0
            })
        } else {
            that.setData({
                hideAdd: 1
            })
        }

    },
    radioChange_sex: function(e) {
        var checkValue_sex = e.detail.value;
        this.setData({
            checkValue_sex: checkValue_sex
        });
        console.log(checkValue_sex)
    },
    sendImg: function(imgurl) {


    },
    chooseImage: function() {
        // 选择图片
        var that = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                let kla = false;

                function admin(tempFilePaths) {
                    console.log("ss");
                    ol = ol + 1;
                    if (ol == 3) {
                        console.log("ss")
                        kla = true
                    } else {
                        console.log("ss")
                        kla = false
                    }
                    return tempFilePaths;
                }
                let ddd = admin(tempFilePaths);
                console.log(ol);
                that.setData({
                    images: that.data.images.concat(ddd),
                    kk: kla
                });
            }
        })

    },
    sendSearch: function(data) {
        // let 
        call.request1(url, postData, function(res) {

        }, function(e) {

        })
    },

    test: function() {
        let kk = this.data.images;
        this.sendImg(kk);
    },
    // 图片预览
    previewImage: function(e) {
        //console.log(this.data.images);
        var current = e.target.dataset.src
        wx.previewImage({
            current: current,
            urls: this.data.images
        })
    },
    delete: function(e) {
        let kk = this;
        var index = e.currentTarget.dataset.index;
        var images = this.data.images;
        images.splice(index, 1);
        this.setData({
            images: images
        });
        ol = ol - 1;
        if (ol != 3) {
            kk.setData({
                kk: false
            })
        }
        console.log(ol)
    },
    up: function() {
        console.log("执行到了")
        let FileName = "";
        FileName = wx.getStorageSync('fileName');
        console.log(FileName)
        let that = this;
        let kk = this.data.images;
        let url = 'Img/addImg?className=searchPet&fileName=null';
        console.log(kk)
        console.log(url)

        //此处是发布数据
        let pushData = "";
        pushData = that.make();
        let data = {
                url: url, //这里是你要上传的服务器接口
                path: kk, //这里是你最开始定义的图片数组
                data: {
                    pushURL: "SearchPet/addSearchPet",
                    pushData: pushData,
                    pushDataChange: function (pushData,FileName) {
                        console.log(FileName);
                        pushData.file_name = FileName;
                        return pushData
                    },//这是个传参函数
                    success: function(res) {
                        console.log(res) //发布成功后会执行此函数
                        if (res.message === "操作成功") {
                            wx.hideLoading();
                            wx.showToast({
                                title: '提交成功',
                                icon: 'success', //如果要纯文本，不要icon，将值设为'none'
                                duration: 1000
                            })
                            setTimeout(() => {
                                wx.navigateBack({
                                    delta: 1, // 回退前 delta(默认为1) 页面
                                })
                            }, 1000)
                        } else {
                            wx.hideLoading();
                            console.log(res)
                            wx.showToast({
                                title: '提交失败',
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 3000
                            })
                        }

                    },
                    fail: function(e) {
                        console.log(e) //发布失败后会执行此函数
                    }
                }
            }
            //上传图片
        call.uploadImage(data);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        console.log("到底了")
    },
    admiin1: function() {

    },
    submit: function() {
        let hh = this;
        wx.showModal({
            title: '提示',
            content: '确认是否提交',
            success: function(res) {
                if (res.confirm) {
                    if (hh.data.images != "" && hh.data.search_pet_name != "" && hh.data.checkValue != "" && hh.data.value != "" && hh.data.search_pet_message != "" && hh.data.publisher_phone != "" && hh.data.dates != "" && hh.data.times != "" && hh.data.lose_city_area != "" && hh.data.checkValue_sex != "" && hh.data.lose_county_area != "" && hh.data.lose_detailed_area != "" && hh.data.special_advice != "") {
                        hh.up();
                        wx.showLoading({
                            title: '正在提交申请',
                        })
                    } else {
                        wx.showModal({
                            title: "温馨提示",
                            content: "请正确填写宠物信息并至少上传一张宠物图片",
                            success: function(res) {
                                if (res.confirm) {

                                } else if (res.cancel) {

                                }
                            }

                        })
                    }
                    // hh.up();
                    // wx.showLoading({
                    //     title: '正在提交申请',
                    // })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },

    make: function() {
        let FileName = "";
        FileName = wx.getStorageSync('fileName');
        let admin = this;
        let SearchM = {
            // "lose_detailed_area": "某街区某小区某栋楼",
            // "lose_time": "2019年9月9日",
            // lose_city_area: "福州",
            // lose_county_area: "鼓楼区",
            // lose_detailed_area: "百度新华街",
            // publisher_phone: "17687551157",
            // search_pet_age: "2月25天",
            // search_pet_category: "喵星人",
            // search_pet_message: "就是很吊？很吊就对了",
            // search_pet_name: "李晓静",
            // search_pet_sex: "女孩子",
            // search_pet_varieties: "埃及猫",
            // special_advice: "就是这么吊？？？？",
        };
        SearchM.special_advice = admin.data.special_advice;
        SearchM.search_pet_name = admin.data.search_pet_name;
        SearchM.search_pet_category = admin.data.checkValue;
        SearchM.search_pet_varieties = admin.data.value;
        // SearchM.search_pet_age = admin.data.search_pet_age;
        SearchM.search_pet_sex = admin.data.checkValue_sex;
        SearchM.lose_county_area = admin.data.lose_county_area;
        SearchM.search_pet_message = admin.data.search_pet_message;
        SearchM.lose_city_area = admin.data.lose_city_area;
        SearchM.lose_detailed_area = admin.data.lose_detailed_area;
        SearchM.lose_time = admin.data.dates + " " + admin.data.times;
        SearchM.publisher_phone = admin.data.publisher_phone;
        SearchM.search_pet_age = admin.data.search_pet_age;
        SearchM.file_name = FileName;
        console.log(SearchM)
        return SearchM;
        // // this.sendSearch(SearchM);
    },
    search_pet_name: function(e) {
        this.setData({
            search_pet_name: e.detail.value
        })
    },
    search_pet_age: function(e) {
        this.setData({
            search_pet_age: e.detail.value
        })
    },
    // search_pet_contact: function (e) {
    //   this.setData({
    //     search_pet_contact: e.detail.value
    //   })
    // },
    search_pet_message: function(e) {
        this.setData({
            search_pet_message: e.detail.value
        })
    },
    search_pet_area: function(e) {
        this.setData({
            search_pet_area: e.detail.value
        })
    },
    publisher_phone: function(e) {
        this.setData({
            publisher_phone: e.detail.value
        })
    },
    // lose_time: function(e) {
    //     this.setData({
    //         lose_time: e.detail.value
    //     })
    // },
    // lose_city_area: function(e) {
    //     this.setData({
    //         lose_city_area: e.detail.value
    //     })
    // },
    // lose_county_area: function(e) {
    //     this.setData({
    //         lose_county_area: e.detail.value
    //     })
    // },
    special_advice: function(e) {
        this.setData({
            special_advice: e.detail.value
        })
        console.log(this.data.special_advice)
    },
    lose_detailed_area: function(e) {
        this.setData({
            lose_detailed_area: e.detail.value
        })
    }


    /**
     * 用户点击右上角分享
     */


})